{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>添加活动</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
        <input type="hidden" name="id" value="{$list.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="title" value="{$list.title}" lay-verify="required" placeholder="请输入活动标题"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">活动分类</label>
            <div class="layui-input-inline">
                <select name="cate_id"  lay-filter="category" lay-search placeholder="请选择活动分类" lay-verify="cate_id" id="category">
                    <option value="1" {if $list.cate_id==1}selected{/if}>普通活动</option>
                    <option value="2" {if $list.cate_id==2}selected{/if}>房间活动</option>
                    <option value="3" {if $list.cate_id==3}selected{/if}>优惠券活动</option>
                    <option value="4" {if $list.cate_id==4}selected{/if}>次卡活动</option>
                    <option value="5" {if $list.cate_id==5}selected{/if}>充值卡活动</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">活动的分类</div>
        </div>
        
        <div class="layui-form-item" id="param"  {if $list.cate_id==1}style="display: none;"{/if}>
            <label class="layui-form-label">关联优惠</label>
            <div class="layui-input-inline" id='coupon_id'>
                
                    {if !$list['param']}
                    
                    {else \}
                    {volist name='list_category' id='vo'}
                        <input 
                        type="checkbox" 
                        name="param" 
                        value="{$vo.id}" 
                        title="{$vo.title}" 
                        {volist name='list["param"]' id="item"}
                            {if $item==$vo.id}
                                checked
                            {/if}
                        {/volist}
                        lay-skin="primary">
                    {/volist}
                    {/if}
             
            </div>
            <div class="layui-form-mid layui-word-aux">选择活动分类下对应的活动产品</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">活动图片</label>
            <input type="hidden" lay-verify="logo" name="image" id="logo2" value="{$list.image}">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="logoBtn2"><i
                            class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="SLogo2" src="{$list.image}">
                        <p id="demoText2"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="start_time" value="" id="start_time" lay-verify="required"
                    placeholder="活动开始时间" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="end_time" value="" id="end_time" lay-verify="required"
                    placeholder="活动结束时间" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-4">
                <input type="radio" name="status" value="0" title="正常" {if $list.status==0}checked{/if}>
                <input type="radio" name="status" value="1" title="下架" {if $list.status==1}checked{/if}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-4" style="width:80%;height:350px;">
                <script id="content" name="content" type="text/plain" style="width:80%; height:400px;">{$list.content}</script>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit=""
                    lay-filter="sys">{:lang('submit')}</button>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    var ue = UE.getEditor('content');
    ue.ready(function () {
         ue.setHeight(200);
        ue.addListener("contentChange", function () {
            $('#content').val(ue.getContent());
        });
    })

    layui.use(['form', 'layer', 'upload','laydate'], function () {
        var form = layui.form, layer = layui.layer, upload = layui.upload, $ = layui.jquery,laydate = layui.laydate;
        form.verify({
            logo: function (value) {
                if (value == '') {
                    return '请上传活动封面图片!';
                }
            },
            cate_id: function (value) {
                if (value == '') {
                    return '请选择活动分类!';
                }
            }

        });




        var uploadInst2 = upload.render({
            elem: '#logoBtn2'
            , url: '{:url("UpFiles/upload")}',
            accept: 'images',
            acceptMime: 'image/*'
            , size: 0
            , multiple: true
            , number: 1
            , before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#SLogo2').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.errorCode == 8888) {
                    $('#logo2').val(res.url);
                } else {
                    $('#SLogo2').attr('src', '');
                    layer.msg(res.msg, { time: 2000, icon: 2 });
                }
            }
        });
        let start_time = laydate.render({
            elem:'#start_time',
            value:new Date({$list.start_time*1000}),
            isInitValue:true,
            format:'yyyy-MM-dd'
        })
        let end_time = laydate.render({
            elem:'#end_time',
            value:new Date({$list.end_time*1000}),
            isInitValue:true,
            format:'yyyy-MM-dd'
        })


        //提交监听
        form.on('select(category)', function (obj) {
            let val = obj.value
            if (val == 1) {
                $('#param').css({ 'display': 'none' })
                $('#coupon_id').html(``)
                form.render();
            } else {
                $('#param').css({ 'display': 'block' })
                $.post('{:url("getparam")}', { cate_id: val }, function (res) {
                    let data = res.data
                    if (data.length > 0) {
                        let leng = data.length
                        $('#coupon_id').html('')
                        for (let i = 0; i < leng; i++) {
                            $('#coupon_id').append(`<input type="checkbox" name="param" value="${data[i].id}" title="${data[i].title}" lay-skin="primary">`)
                        }
                        form.render();
                    }
                })
            }
        });
        form.on('submit(sys)', function (data) {
            $("#submit").attr({ disabled: "disabled" });
            loading = layer.load(1, { shade: [0.1, '#fff'] });
            if (data.field.start_time) {
                data.field.start_time = new Date(data.field.start_time) / 1000
            }
            if (data.field.end_time) {
                data.field.end_time = new Date(data.field.end_time) / 1000
            } else {
                data.field.end_time = 0
            }
            if(data.field.cate_id==1){
                delete data.field.param
            }
            let arr = []
            $("input:checkbox[name='param']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            data.field.param = arr.toString()
            $.post("{:url('add')}", data.field, function (res) {
                layer.close(loading);
                if (res.errorCode == 8888) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        location.href = '{:url("index")}';
                    });
                } else {
                    $("#submit").removeAttr("disabled");
                    layer.msg(res.msg, { icon: 2, time: 1000 });
                }
            });
        })
    })


</script>


</body>

</html>